package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/2
 */
@DubboService
public class VideoApiImpl implements VideoApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;

    @Override
    public String saveVideo(Video video) {
        //设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //保存对象
        mongoTemplate.save(video);
        //构造返回值
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public List<Video> getFollowVideos(Long userId, Integer followCount) {
        Query query = Query.query(Criteria.where("userId").ne(userId))
                .limit(followCount)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public List<Video> getVideos(Long userId, Integer page, Integer pagesize, Integer followCount) {
        Long skip = (long) (page - 1) * pagesize - followCount;
        Query query = Query.query(Criteria.where("userId").ne(userId))
                .skip(skip)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public Boolean hasFocused(Long userId, Long followUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("followUserId").is(followUserId);
        return mongoTemplate.exists(Query.query(criteria), FocusUser.class);
    }

    @Override
    public void saveFocusUser(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
    }

    @Override
    public void removeFocusUser(Long userId, Long followUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("followUserId").is(followUserId);
        mongoTemplate.remove(Query.query(criteria), FocusUser.class);
    }

    @Override
    public PageResult getVideoList(Integer page, Integer pagesize, Long uid) {
        Criteria criteria = new Criteria();
        if (ObjectUtil.isNotEmpty(uid)) {
            criteria.and("userId").is(uid);
        }
        Long count = mongoTemplate.count(Query.query(criteria), Video.class);
        Long skip = (long)(page - 1) * pagesize;
        Query query = Query.query(criteria)
                .skip(skip)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> videos = mongoTemplate.find(query, Video.class);
        if (CollUtil.isEmpty(videos)) {
            return new PageResult();
        }
        return new PageResult(page, pagesize, Integer.parseInt(count.toString()), videos);
    }
}
